package cn.jtfadmin.base.lang.common.domain.dto;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 分页查询结果
 *
 * @author akfh
 * @since 1.0
 */
@Getter
@Setter
@ToString
public class PageRDTO<T> implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /**
     * 内容
     */
    protected List<T> content;

    /**
     * 总数量
     */
    protected Long totalElements;


    public PageRDTO() {
    }

    public PageRDTO(List<T> content, Long totalElements) {
        this.content = content;
        this.totalElements = totalElements;
    }

    public <V> PageRDTO<V> map(Function<T,V> function){
        PageRDTO<V> pageRDTO = new PageRDTO<>();
        pageRDTO.setTotalElements(this.totalElements);
        if(content == null){
            pageRDTO.setContent(null);
        }else {
            pageRDTO.setContent(content.stream().map(t->function.apply(t)).collect(Collectors.toList()));
        }
        return pageRDTO;
    }
}
